// miniprogram/pages/applications/addticket/addticket.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [{
        name: 'yes',
        value: '是',
        checked: 'true'
      },
      {
        name: 'no',
        value: '否'
      },
    ]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.initSelectTime();
  },
  //初始化选择时间
  initSelectTime() {
    let time = new Date();
    let yy = time.getFullYear(),
      mm = time.getMonth() + 1 > 9 ? time.getMonth() + 1 : '0' + (time.getMonth() + 1),
      dd = time.getDate() > 9 ? time.getDate() : '0' + time.getDate();
    this.setData({
      stime: yy + '-' + mm + '-' + dd,
      etime: (yy + 1) + '-' + mm + '-' + dd,
      nstime: yy + '-' + mm + '-' + dd,
    })
  },
  bindStartDate(e) {
    console.log(e)
    var val = e.detail.value;
    this.setData({
      start_time: val,
      nstime: val
    })
  },
  bindEndDate(e) {
    console.log(e)
    let val = e.detail.value;
    this.setData({
      end_time: val
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  radioChange(e) {
    console.log(e);
    let val = e.detail.value;
    if (val == 'no') {
      this.getTypeList();
      this.setData({
        use_all: false,
      })
    } else {
      this.setData({
        use_all: true
      })
    }
  },
  getTypeList() {
    let _this = this;
    let db = wx.cloud.database();
    db.collection('tb_type_list').get({
      success: res => {
        console.log(res)
        _this.setData({
          type_list: res.data
        })
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  getSortList(e) {
    let idx = e.currentTarget.dataset.idx;
    let types = this.data.type_list;
    this.setData({
      sid: idx,
      types: types[idx]
    })
  },
  formSubmit(e) {
    console.log(e);
    let val = e.detail.value;
    for (let i in val) {
      if (i != 'use_all' && i != 'type_name' && !val[i]) {
        return wx.showToast({
          title: '请检查表单',
        })
      }
    }
    console.log('表单验证通过');
    this.addTickets(val);
  },
  //添加购物券
  addTickets(data) {
    let _this = this;
    let db = wx.cloud.database();
    db.collection('tb_ticket_list').add({
      data: data,
      success: res => {
        console.log(res)
        wx.showToast({
          title: '添加成功',
        })
      },
      fail: err => {
        console.log(err)
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})